package mo.tags.stack;

import java.util.Stack;

public class L2434 {

    public String robotWithString(String s) {
        StringBuilder builder = new StringBuilder();
        char[] chars = s.toCharArray();
        int[] cnt = new int[26];
        for (char c : chars) {
            cnt[c-'a']++;
        }
        int min = 0;
        Stack<Character> stack = new Stack<>();
        for (char c : chars) {
            cnt[c-'a']--;
            while (min < 25 && cnt[min] == 0) {
                min++;
            }
            stack.push(c);
            while (!stack.isEmpty() && stack.peek()-'a' <= min) {
                builder.append(stack.pop());
            }
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        L2434 test = new L2434();
        System.out.println(test.robotWithString("bdd"));
    }
}
